const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        canCommit: false,
        phone: '',
        timer: '', //定时器名字
        countDownNum: '60', //倒计时初始值
        countDown: false,
        codeDisable: true
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let self = this;
        wx.getStorage({
            key: 'key',
            success: (res) => {
                self.setData({
                    member_key: res.data
                });
                this.getPhoneNum();
            }
        })
    },
    /**
     * 获取手机号
     */
    getPhoneNum: function () {
        var self = this;
        wx.request({
            url: app.globalData.ApiUrl + '/index.php?act=member_account&op=get_mobile_info&key=' + self.data.member_key,
            success(res) {
                self.setData({
                    phone: res.data.datas.mobile
                });
            }
        });
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        //初始化图形验证码
        // this.check = this.selectComponent("#check");
        // this.check.drawPic();
    },

    /*
     *监听充值卡的输入
     */
    cardInput: function (event) {
        var self = this;
        if (event.detail.value.length > 0) {
            self.data.canCommit = true;
        } else {
            self.data.canCommit = false;
        }
        self.setData({
            canCommit: self.data.canCommit,
        });
    },
    /**
     * 卡片提交
     */
    commitPhone: function (event) {
        // var self = this;
        // self.check.drawPic();
        // self.check.showDialog(event.detail);
        this.getMessageCode()

    },
    //图像验证码确认事件
    // _vertifySuccess(detail) {
    //     this.getMessageCode();
    // },
    /**
     * 提交短信验证码  下一步
     */
    commitCode: function (event) {
        var self = this;
        if (!self.data.canCommit) {
            return;
        }
        let phoneCode = event.detail.value.cardn;
        wx.showLoading({
            title: "加载中",
        })
        wx.request({
            url: app.globalData.ApiUrl + '/index.php?act=member_account&op=modify_password_step3',
            method: "POST",
            data: {
                key: self.data.member_key,
                auth_code: phoneCode
            },
            header: {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            success: function (res) {
                if (res.data.code == 200) {
                    wx.navigateTo({
                        url: '../member_update_pwd2/member_update_pwd2'
                    })
                } else {
                    wx.showToast({
                        title: res.data.datas.error,
                        icon: 'none'
                    })
                }
            },
            fail: function (res) {
                wx.showToast({
                    title: '网络请求失败',
                    icon: 'none'
                })
            }
        })
    },
    /**
     * 获取短信验证码
     */
    getMessageCode: function () {
        var self = this;
        wx.showLoading({
            title: "加载中",
        })
        wx.request({
            url: app.globalData.ApiUrl + '/index.php?act=member_account&op=modify_password_step2',
            method: "POST",
            data: {
                key: self.data.member_key,
            },
            header: {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            success: function (res) {
                if (res.data.code == 200) {
                    self.data.countDownNum = res.data.datas.sms_time;
                    self.countDown();
                    self.setData({
                        codeDisable: false
                    });
                    wx.showToast({
                        title: '验证码已发送!',
                        icon: 'success'
                    })
                } else {
                    wx.showToast({
                        title: res.data.datas.error,
                        icon: 'none'
                    })
                }
            },
            fail: function (res) {
                wx.showToast({
                    title: '网络请求失败',
                    icon: 'none'
                })
            }
        })
    },
    /**
     * 验证码倒计时
     */
    countDown: function () {
        let that = this;
        that.setData({
            countDown: true
        })
        let countDownNum = that.data.countDownNum; //获取倒计时初始值
        //如果将定时器设置在外面，那么用户就看不到countDownNum的数值动态变化，所以要把定时器存进data里面
        that.setData({
            timer: setInterval(function () { //这里把setInterval赋值给变量名为timer的变量
                //每隔一秒countDownNum就减一，实现同步
                countDownNum--;
                //然后把countDownNum存进data，好让用户知道时间在倒计着
                that.setData({
                    countDownNum: countDownNum
                })
                //在倒计时还未到0时，这中间可以做其他的事情，按项目需求来
                if (countDownNum == 0) {
                    //这里特别要注意，计时器是始终一直在走的，如果你的时间为0，那么就要关掉定时器！不然相当耗性能
                    //因为timer是存在data里面的，所以在关掉时，也要在data里取出后再关闭
                    clearInterval(that.data.timer);
                    //关闭定时器之后，可作其他处理codes go here
                    that.setData({
                        countDown: false
                    })
                }
            }, 1000)
        })
    }
})